/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/11/2
 * Time: 10:32
 * Description:
 */
public class MyStack {
    public int[] elem;
    public int usedSize;
    public MyStack() {
        this.elem = new int[5];
    }
    //压栈
    public void push(int val) {
        if(isFull()) {
            System.out.println("栈满了！");
            return;
        }
        this.elem[this.usedSize++] = val;
    }

    public boolean isFull() {
        return this.usedSize == this.elem.length;
    }
    //弹出栈顶元素
    public int pop() {
        if(empty()) {
            //return -1;
            throw new RuntimeException("栈为空！");
        }
        int val = this.elem[this.usedSize-1];
        this.usedSize--;
        return val;
    }

    public boolean empty() {
        return this.usedSize == 0;
    }
    //只是获取栈顶元素 但是不删除
    public int peek() {
        if(empty()) {
            //return -1;
            throw new RuntimeException("栈为空！");
        }
        return this.elem[this.usedSize-1];
    }
}
